const app = getApp()

import {
  GetGoodsDetail,
  SetPosterGoods
} from '../../../config/api'
import util from '../../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    model: '',
    id: '', // 商品id
    share_flag: true, // 分享底层遮罩隐藏
    hideShareImg: true, // 分享海报展示区域隐藏
    goodsDetail: {},
    richText: '', //富文本内容数据
    share_img: '', // 小程序分享图片
    tempFilePath: '',

    flag: false, //是否显示遮罩，默认不显示
    num: 1, //购买数量，默认为1
    price: 0, //每件物品价格，默认为0
    img: '', //选中的物品图片
    goodsname: '', //单个物品名称,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);

    let model = wx.getStorageSync('model')
    this.setData({
      model
    });

    if (options.scene) {
      const scene = decodeURIComponent(options.scene)
      this.setData({
        id: scene
      })
    } else {
      console.log('正常进入')
      this.setData({
        id: options.id
      })
    }

    let that = this;

    util.apiRequest(GetGoodsDetail, {
      id: that.data.id
    }, null).then(r => {
      console.log('商品详情数据 为：', r.data.data);
      that.setData({
        goodsDetail: r.data.data,
        richText: r.data.data.desc.replace(/\<img/gi, '<img width="100%" class="rich-img" '),
      })
    });
  },

  setPosterGoods() { // 生成分享码
    let that = this;
    let params = {
      vollege_id: app.globalData.vollege_id,
      scene: that.data.id,
      page_url: 'pages/goods/goods_detail/goods_detail',
      goods_id: that.data.id
    }

    console.log('参数', params)

    util.apiRequest(SetPosterGoods, params, null).then(r => {
      console.log('生成结果：', r)
      that.setData({
        share_img: r.data.data,
        hideShareImg: false,
        share_flag: true
      })

      wx.downloadFile({
        url: that.data.share_img, //
        success(res) {
          console.log('11111', res)
          // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
          if (res.statusCode === 200) {
            let img = res.tempFilePath
            that.setData({
              tempFilePath: res.tempFilePath
            })
          }
        }
      })

    })
  },

  onDownoadImg() { // 下载海报
    let that = this;
    wx.saveImageToPhotosAlbum({
      filePath: this.data.tempFilePath,
      success(res) {
        wx.showToast({
          title: '下载成功',
        })
        that.setData({
          hideShareImg: true
        })
      }
    })

  },

  onShare() {
    this.setData({
      share_flag: false
    })
  },

  // 遮罩层隐藏
  conceal: function() {
    this.setData({
      share_flag: true
    })
  },

  /**
   * 购买
   */
  BuyNow: function() {
    console.log('您点击了购买！');
    // 跳转到购买页面
    wx.navigateTo({
      url: '/pages/goods/buygoods/buygoods?num=' + this.data.num + '&price=' + this.data.price + '&img=' + this.data.img + '&name=' + this.data.goodsname + '&id=' + this.data.goodsDetail.id,
    });

  },
  /**
   * 增加
   */
  Add: function() {
    let that = this;
    console.log('您点击了增加！', that.data.num);
    if (that.data.num >= 0) {
      that.setData({
        num: that.data.num + 1
      });
    }
  },
  /**
   * 减少
   */
  Reduce: function() {
    let that = this;
    console.log('您点击了减少！', that.data.num);
    if (that.data.num <= 1) {
      that.setData({
        num: 1
      });
    }
    // else if(that.data.num==0){
    //     that.setData({
    //         num:0
    //     });
    // }
    else {
      that.setData({
        num: that.data.num - 1
      });
    }
  },


  /**
   * 购买商品
   */
  Buy: function() {
    console.log('您点击了购买商品！');
    let that = this;
    that.setData({
      flag: true,
      img: that.data.goodsDetail.img,
      price: that.data.goodsDetail.price,
      goodsname: that.data.goodsDetail.title,

    });
  },
  /**
   * 支付调用
   */
  //取消遮罩
  cancelZhezao: function(e) {
    console.log('取消遮罩');
    let that = this;
    that.setData({
      flag: false,
      num: 1,
      price: 0,
      goodsname: ''
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})